<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否审核" prop="isCheck">
        <el-select v-model="queryParams.isCheck" placeholder="请选择是否审核" clearable size="small">
          <el-option
            v-for="dict in isCheckOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上架拍买" prop="isShopping">
        <el-select v-model="queryParams.isShopping" placeholder="请选择上架拍买" clearable size="small">
          <el-option
            v-for="dict in isShoppingOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:auction:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:auction:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:auction:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:auction:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="auctionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="上架拍买" align="center" prop="id" />
      <el-table-column label="名称" :show-overflow-tooltip="true" align="center" prop="name" />
      <el-table-column label="唯一拍卖码" :show-overflow-tooltip="true" align="center" prop="auctionCode" />
      <el-table-column label="描述" :show-overflow-tooltip="true" align="center" prop="detail" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拍卖底价" align="center" prop="basePrice" />
      <el-table-column label="拍卖押金" align="center" prop="securityPrice" />
      <el-table-column label="加价幅度" align="center" prop="rangePrice" />
      <el-table-column label="发布者" align="center" prop="publisher" />
      <el-table-column label="是否审核" align="center" prop="isCheck" :formatter="isCheckFormat" />
      <el-table-column label="上架拍买" align="center" prop="isShopping" :formatter="isShoppingFormat" />
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:auction:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:auction:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改拍卖管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="拍卖唯一标识码" prop="name">
          <el-input v-model="form.auctionCode" readonly />
        </el-form-item>
        <el-form-item label="描述" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="拍卖底价" prop="basePrice">
          <el-input v-model="form.basePrice" placeholder="请输入拍卖底价" />
        </el-form-item>
        <el-form-item label="拍卖押金" prop="securityPrice">
          <el-input v-model="form.securityPrice" placeholder="请输入拍卖押金" />
        </el-form-item>
        <el-form-item label="加价幅度" prop="rangePrice">
          <el-input v-model="form.rangePrice" placeholder="请输入加价幅度" />
        </el-form-item>
<!--        <el-form-item label="发布者" prop="publisher">-->
<!--          <el-input v-model="form.publisher" placeholder="请输入发布者" />-->
<!--        </el-form-item>-->
        <el-form-item label="图片地址" prop="publisher">
          <!--          <el-input v-model="form.url" placeholder="请输入分类icon" />-->
          <el-upload
            :headers="videoUpload.headers"
            :action="videoUpload.url"
            :file-list="poster"
            :before-upload="limit_1"
            list-type="picture-card"
            :on-success="handlePagePhotoSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handlePagePhotoRemove"
            accept=".jpg,.jpeg,.png,,.JPG,.JPEG"
            :limit="9">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
<!--         <el-form-item label="图片地址" prop="publisher">-->
<!--          <el-input v-model="form.url" placeholder="图片地址" />-->
<!--        </el-form-item>-->
        <el-form-item label="是否审核">
          <el-radio-group v-model="form.isCheck">
            <el-radio
              v-for="dict in isCheckOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上架拍买">
          <el-radio-group v-model="form.isShopping">
            <el-radio
              v-for="dict in isShoppingOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAuction, getAuction, delAuction, addAuction, updateAuction, exportAuction } from "@/api/content/auction";
import { getToken } from '@/utils/auth'
export default {
  name: "Auction",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 拍卖管理表格数据
      auctionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否审核字典
      isCheckOptions: [],
      // 上架拍买字典
      isShoppingOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        detail: undefined,
        createTime: undefined,
        startTime: undefined,
        endTime: undefined,
        basePrice: undefined,
        securityPrice: undefined,
        rangePrice: undefined,
        publisher: undefined,
        isCheck: undefined,
        isShopping: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      videoUpload:{
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sys/file/upload",
        url2: process.env.VUE_APP_BASE_API,
      },
      poster:[],
      t1:[]
    };
  },
  created() {
    this.getList();
    this.getDicts("video_is_check").then(response => {
      this.isCheckOptions = response.data;
    });
    this.getDicts("sys_on_status").then(response => {
      console.log( response.data)
      this.isShoppingOptions = response.data;
    });
  },
  methods: {
    /** 查询拍卖管理列表 */
    getList() {
      this.loading = true;
      listAuction(this.queryParams).then(response => {
        this.auctionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否审核字典翻译
    isCheckFormat(row, column) {
      return this.selectDictLabel(this.isCheckOptions, row.isCheck);
    },
    // 上架拍买字典翻译
    isShoppingFormat(row, column) {
      return this.selectDictLabel(this.isShoppingOptions, row.isShopping);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        detail: undefined,
        createTime: undefined,
        startTime: undefined,
        endTime: undefined,
        basePrice: undefined,
        securityPrice: undefined,
        rangePrice: undefined,
        publisher: undefined,
        isCheck: "0",
        isShopping: "0",
        url:undefined
      };
      this.poster = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拍卖管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAuction(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拍卖管理";
        console.log(response.data);
        this.poster = JSON.parse(response.data.url).map(item => {
          return {
            url: item
          }
        });
        this.t1 = JSON.parse(response.data.url);
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        console.log(this.t1)
        this.form.url =JSON.stringify(this.t1)
        if (valid) {
          if (this.form.id != undefined) {
            updateAuction(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addAuction(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除拍卖管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAuction(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有拍卖管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAuction(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handlePagePhotoRemove(file, fileList) {
      console.log("移除")
      console.log(fileList)
      console.log(file);
      this.poster = fileList;
      this.t1 = [];
      for(var i = 0;i<fileList.length;i++){
        this.t1.push(fileList[i].url)
      }
    },

    handlePagePhotoSuccess(res, file){
      console.log(file)
      this.poster.push(file)
      this.t1.push(file.response.url)
    },
    handlePictureCardPreview(file) {
      console.log("预览")

    },
    getUrl(v){
      var  arr = new Array();
      for (var i =0;i<v.length;i++){
        console.log("是否存在"+v[i].response)
        if (v[i].response==null||v[i].response==undefined){
          arr.push(v[i].url)
        }else {
          arr.push(v[i].response)
        }
      }
      return arr;
    },
    limit_1(file){
      this.msgInfo("不能大于9张")
    },
  }
};
</script>
